package cn.nawang.ebeim.server.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nawang.ebeim.server.constants.ApiConstants;
import cn.nawang.ebeim.server.constants.Config;
import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.server.constants.Constant;
import cn.nawang.ebeim.server.entity.DataSource;
import cn.nawang.ebeim.server.entity.StorageCloud;
import cn.nawang.ebeim.server.entity.StorageFile;
import cn.nawang.ebeim.server.entity.StorageFileToDo;
import cn.nawang.ebeim.server.entity.StorageFileToDoView;
import cn.nawang.ebeim.server.entity.StorageServer;
import cn.nawang.ebeim.server.entity.StorageStrategy;
import cn.nawang.ebeim.server.entity.StorageVersionTree;
import cn.nawang.ebeim.server.entity.StorageVersionTreeToDo;
import cn.nawang.ebeim.server.entity.StorageVersionTreeToDoView;
import cn.nawang.ebeim.server.entity.TbStorageStrategy;

/**
 * @ClassName: ApiVisitUtil
 * @Description: api调用方法
 * @author fanyb
 * @date Aug 17, 2015 4:37:38 PM
 * @version v1.0
 */
public class ApiVisitUtil {
  /**
   * 
   * @author fanyb
   * @Description: 调用服务器方法封装
   * @param URL 服务器提供的路径
   * @param method 提交的方法形式
   * @param map 传入的参数
   * @return String string对象
   * @throws Exception
   * @date Aug 17, 20152:04:43 PM
   *
   * @修改人
   * @修改日期
   */
  public static String doServerMethod(String URL, String method, Map<String, Object> map)
      throws Exception {
    // 获取到没有带参数的路径
    String urlNoParam = URL;
    // 使用的url
    String url = "";
    // 查询参数
    String content = "";
    if (map != null) {
      for (Map.Entry<String, Object> entry : map.entrySet()) {
        if (entry.getValue() != null) {
          content +=
              entry.getKey() + "=" + URLEncoder.encode(entry.getValue().toString(), "utf-8") + "&";
        }
      }
      // 最后一个字符位置不能带&符号，
      content = content.substring(0, content.lastIndexOf("&"));
    }
    // 如果提交方法是post,则url不带参数
    if (("post").equals(method.toLowerCase())) {
      url = urlNoParam+"?"+ ApiConstants.TOKEN;
    } else {// 提交方法是get,带参数
      url = urlNoParam + "?" + content+"&"+ApiConstants.TOKEN;
    }
    URL localURL = new URL(url);
    URLConnection connection = localURL.openConnection();
    HttpURLConnection httpURLConnection = (HttpURLConnection) connection;

    httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
    httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    // post方法参数提交方式
    if (("post").equals(method.toLowerCase())) {
      // Output to the connection. Default is
      // false, set to true because post
      // method must write something to the
      // connection
      // 设置是否向connection输出，因为这个是post请求，参数要放在
      // http正文内，因此需要设为true
      httpURLConnection.setDoOutput(true);
      // Read from the connection. Default is true.
      httpURLConnection.setDoInput(true);
      // Set the post method. Default is GET
      httpURLConnection.setRequestMethod("POST");
      // Post cannot use caches
      // Post 请求不能使用缓存
      httpURLConnection.setUseCaches(false);
      DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
      out.writeBytes(content);
      out.flush();
      out.close();
    }
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader reader = null;
    StringBuffer resultBuffer = new StringBuffer();
    String tempLine = null;

    if (httpURLConnection.getResponseCode() >= 300) {
      throw new Exception(
          "HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
    }

    try {
      inputStream = httpURLConnection.getInputStream();
      inputStreamReader = new InputStreamReader(inputStream);
      reader = new BufferedReader(inputStreamReader);

      while ((tempLine = reader.readLine()) != null) {
        resultBuffer.append(tempLine);
      }

    } finally {

      if (reader != null) {
        reader.close();
      }

      if (inputStreamReader != null) {
        inputStreamReader.close();
      }

      if (inputStream != null) {
        inputStream.close();
      }

    }
    return resultBuffer.toString();
  }

  /**
   * 
   * @author fanyb
   * @Description: 将api返回的json转换成列表
   * @param jsonText api返回的json
   * @param cls 类名
   * @return List<Object>
   * @date Aug 17, 20154:47:08 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> str2List(String jsonText, Class cls) {
    if (jsonText == null || "".equals(jsonText)) return null;
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      List<T> list = JsonUtil.toList(obj.getString("list"), cls);
      return list;
    }
    return null;
  }

  /**
   * 
   * @author fanyb
   * @Description: 实体转换成map格式的值
   * @param obj 实体对象
   * @return Map<String,Object> 实体属性map格式值
   * @date Aug 19, 20155:06:21 PM
   *
   * @修改人
   * @修改日期
   */
  public static Map<String, Object> objectToMap(Object obj) {
    Map<String, Object> map = new HashMap<String, Object>();
    // 得到类对象
    Class cls = (Class) obj.getClass();
    // 得到类中的所有属性集合
    Field[] ds = cls.getDeclaredFields();
    for (int i = 0; i < ds.length; i++) {
      Field f = ds[i];
      f.setAccessible(true); // 设置些属性是可以访问的
      // 获取属性的名字
      String name = ds[i].getName();
      // 将属性的首字符大写，方便构造get，set方法
      name = name.substring(0, 1).toUpperCase() + name.substring(1);
      try {
        // 得到此属性的值
        Object val = f.get(obj);
        map.put(name, val);
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    }
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据客户编号获取存储策略
   * @param customerId 客户编号
   * @return BackStrategy 存储策略
   * @date Aug 24, 20155:58:55 PM
   *
   * @修改人
   * @修改日期
   */
  public static StorageStrategy getStorageStrategyByDatasourceId(String customerId) {
    StorageStrategy storageStrategy = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("customerId", customerId);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_STORAGE_STRATEGY_BY_CUSTOMER_ID, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return null;
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("object") != null) {
      storageStrategy =
          (StorageStrategy) JsonUtil.toObject(obj.getString("object"), StorageStrategy.class);
      return storageStrategy;
    }
    return storageStrategy;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据存储云编号数组获取存储云列表
   * @param storageCloudId 存储云编号(如果有多个就用“,”分隔开的字符串)
   * @return List<T> 存储云列表
   * @throws Exception
   * @date Aug 26, 201511:52:22 AM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> findByStorageCloudId(String storageCloudId) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageCloudId", storageCloudId);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_STORAGE_CLOUD_BY_STORAGE_CLOUD_ID, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    List<T> list = str2List(jsonText, StorageCloud.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新数据源
   * @param datasource 数据源
   * @return String 更新結果
   * @date Aug 19, 20155:15:10 PM
   *
   * @修改人
   * @修改日期
   */
  public static String updataDatasource(DataSource datasource) {
    Map<String, Object> map = objectToMap(datasource);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_UPDATE_DATASOURCE, ApiConstants.POST, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增storageFileToDo
   * @param storageFileToDo 对象
   * @return String 新增结果
   * @date Aug 26, 20155:19:01 PM
   *
   * @修改人
   * @修改日期
   */
  public static String addStorageFileToDo(StorageFileToDo storageFileToDo) {
    Map<String, Object> map = objectToMap(storageFileToDo);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_ADD_STORAGE_FILE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 批量删除StorageFileToDo
   * @param storageFileToDoIds 多个storageFileToDoId则以逗号隔开
   * @return String 删除结果
   * @date Aug 26, 20155:23:47 PM
   *
   * @修改人
   * @修改日期
   */
  public static String deleteStorageFileToDo(String storageFileToDoIds) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageFileToDoIds", storageFileToDoIds);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_DELETE_STORAGE_FILE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找未处理StorageFileToDo列表
   * @return List<T>
   * @date Aug 26, 20155:27:46 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> getStorageFileToDoList() {
    Map<String, Object> map = null;
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_FIND_UNTREATED_STORAGE_FILE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    List<T> list = str2List(jsonText, StorageFileToDoView.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增StorageVersionTreeToDo
   * @param storageVersionTreeToDo 对象
   * @return String 新增结果
   * @date Aug 26, 20155:19:01 PM
   *
   * @修改人
   * @修改日期
   */
  public static String addStorageVersionTreeToDo(StorageVersionTreeToDo storageVersionTreeToDo) {
    Map<String, Object> map = objectToMap(storageVersionTreeToDo);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_ADD_STORAGE_VERSION_TREE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 批量删除StorageVersionTreeToDo
   * @param storageVersionTreeToDoIds 多个storageVersionTreeToDoId则以逗号隔开
   * @return String 删除结果
   * @date Aug 26, 20155:23:47 PM
   *
   * @修改人
   * @修改日期
   */
  public static String deleteStorageVersionTreeToDo(String storageVersionTreeToDoIds) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageVersionTreeToDoIds", storageVersionTreeToDoIds);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_DELETE_STORAGE_VERSION_TREE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找未处理StorageFileToDo列表
   * @return List<T>
   * @date Aug 26, 20155:27:46 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> getStorageVersionTreeToDoList() {
    Map<String, Object> map = null;
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_FIND_UNTREATED_STORAGE_VERSION_TREE_TO_DO,
          ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    List<T> list = str2List(jsonText, StorageVersionTreeToDoView.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增StorageFile
   * @param storageFile 对象
   * @return String 新增结果
   * @date Aug 26, 20155:19:01 PM
   *
   * @修改人
   * @修改日期
   */
  public static String addStorageFile(StorageFile storageFile) {
    Map<String, Object> map = objectToMap(storageFile);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_ADD_STORAGE_FILE, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取数据源对象
   * @param id 数据源编号
   * @return DataSource 数据源对象
   * @date Aug 20, 201511:40:58 AM
   *
   * @修改人
   * @修改日期
   */
  public static DataSource getDataSource(String id) {
    DataSource dataSource = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", id);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_GET_DATASOURCE, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return null;
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("obj") != null) {
      dataSource = (DataSource) JsonUtil.toObject(obj.getString("obj"), DataSource.class);
      return dataSource;
    }
    return dataSource;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取服务端列表
   * @return List<T> 服务端列表
   * @date Aug 28, 20159:37:06 AM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> getStorageServerList() {
    Map<String, Object> map = null;
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_FIND_STORAGE_SERVICE, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    List<T> list = str2List(jsonText, StorageServer.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增StorageVersionTree
   * @param storageVersionTree 新增对象
   * @return String
   * @date Aug 28, 201510:49:46 AM
   *
   * @修改人
   * @修改日期
   */
  public static String addStorageVersionTree(StorageVersionTree storageVersionTree) {
    Map<String, Object> map = objectToMap(storageVersionTree);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_ADD_STORAGE_VERSION_TREE, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据云存储名称获取云存储对象
   * @param name 云存储名称
   * @return StorageCloud
   * @date Sep 16, 201511:35:42 AM
   *
   * @修改人
   * @修改日期
   */
  public static StorageCloud getStorageCloudByName(String name) {
    StorageCloud storageCloud = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", name);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_GET_STORAGE_CLOUD_BY_NAME, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return null;
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("obj") != null) {
      storageCloud = (StorageCloud) JsonUtil.toObject(obj.getString("obj"), StorageCloud.class);
      return storageCloud;
    }
    return storageCloud;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取云存储的前端列表
   * @param storageCloudId 云编号
   * @return List<Object> 云存储的前端列表
   * @throws Exception
   * @date Aug 18, 20156:07:20 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> getStorageCloudServerList(String storageCloudId) throws Exception {

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageCloudId", storageCloudId);
    String jsonText = doServerMethod(ApiConstants.URL_SSERVER_LIST, ApiConstants.GET, map);
    List<T> list = str2List(jsonText, StorageServer.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新StorageFileToDo
   * @param storageFileToDoView
   * @return String
   * @date Sep 16, 20152:51:41 PM
   *
   * @修改人
   * @修改日期
   */
  public static String updataStorageFileToDo(StorageFileToDoView storageFileToDoView) {
    Map<String, Object> map = objectToMap(storageFileToDoView);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_UPDATE_STORAGE_FILE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新StorageFileToDo
   * @param storageFileToDo
   * @return String
   * @date Sep 16, 20152:51:41 PM
   *
   * @修改人
   * @修改日期
   */
  public static String updataStorageFileToDo(StorageFileToDo storageFileToDo) {
    Map<String, Object> map = objectToMap(storageFileToDo);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_UPDATE_STORAGE_FILE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新StorageVersionTreeToDo
   * @param storageVersionTreeToDoView
   * @return String
   * @date Sep 16, 20152:52:55 PM
   *
   * @修改人
   * @修改日期
   */
  public static String updataStorageVersionTreeToDo(
      StorageVersionTreeToDoView storageVersionTreeToDoView) {
    Map<String, Object> map = objectToMap(storageVersionTreeToDoView);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_UPDATE_STORAGE_VERSION_TREE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新StorageVersionTreeToDo
   * @param storageVersionTreeToDo
   * @return String
   * @date Sep 16, 20152:52:55 PM
   *
   * @修改人
   * @修改日期
   */
  public static String updataStorageVersionTreeToDo(StorageVersionTreeToDo storageVersionTreeToDo) {
    Map<String, Object> map = objectToMap(storageVersionTreeToDo);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_UPDATE_STORAGE_VERSION_TREE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 判断storageVersionTreeToDo是否存在
   * @param storageCloudId
   * @param datasourceId
   * @param version
   * @return String
   * @date Sep 16, 20156:15:47 PM
   *
   * @修改人
   * @修改日期
   */
  public static String isExistStorageVersionTreeToDo(String storageCloudId, String datasourceId,
      String version) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageCloudId", storageCloudId);
    map.put("datasourceId", datasourceId);
    map.put("version", version);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_IS_EXIST_STORAGE_VERSION_TREE_TO_DO,
          ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && "false".equals(obj.get("message").toString())) {
      return Constant.NO_EXIST;
    } else if ("200".equals(obj.get("code").toString())
        && "true".equals(obj.get("message").toString())) {
      return Constant.EXIST;
    }
    return "false";
  }

  public static String isExistStorageFileToDo(String storageCloudId, String datasourceId,
      String signature) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageCloudId", storageCloudId);
    map.put("datasourceId", datasourceId);
    map.put("signature", signature);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_IS_EXIST_STORAGE_FILE_TO_DO, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && "false".equals(obj.get("message").toString())) {
      return Constant.NO_EXIST;
    } else if ("200".equals(obj.get("code").toString())
        && "true".equals(obj.get("message").toString())) {
      return Constant.EXIST;
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: TODO
   * @param datasourceId
   * @param signature
   * @return StorageFileToDo
   * @date Sep 17, 20159:35:58 AM
   *
   * @修改人
   * @修改日期
   */
  public static StorageFileToDo getStorageFileToDoByDsIdAndSignature(String datasourceId,
      String signature) {
    StorageFileToDo storageFileToDo = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("datasourceId", datasourceId);
    map.put("signature", signature);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_FIND_STORAGE_FILE_TO_DO_BY_DSID_AND_SIGN,
          ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return null;
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("obj") != null) {
      storageFileToDo =
          (StorageFileToDo) JsonUtil.toObject(obj.getString("obj"), StorageFileToDo.class);
      return storageFileToDo;
    }
    return storageFileToDo;
  }

  public static StorageVersionTreeToDo getStorageVersionTreeToDoByDsIdAndVersion(
      String datasourceId, String version) {
    StorageVersionTreeToDo storageVersionTreeToDo = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("datasourceId", datasourceId);
    map.put("version", version);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_FIND_STORAGE_VERSION_TREE_TO_DO_BY_DSID_VERS,
          ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return null;
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("obj") != null) {
      storageVersionTreeToDo = (StorageVersionTreeToDo) JsonUtil.toObject(obj.getString("obj"),
          StorageVersionTreeToDo.class);
      return storageVersionTreeToDo;
    }
    return storageVersionTreeToDo;
  }

  public static <T> List<T> getTbStorageStrategyByCustomerId(String customerId) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("customerId", customerId);
    String jsonText = "";
    try {
      jsonText = doServerMethod(ApiConstants.URL_TB_STORAGE_STRATEGY_BY_CUSTOMER_ID,
          ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    List<T> list = str2List(jsonText, TbStorageStrategy.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 判断TbStorageStrategy是否存在
   * @param storageCloudId
   * @return String 
   * @date Oct 12, 20152:09:10 PM
   *
   * @修改人 
   * @修改日期
   */
  public static String isExistTbStorageStrategy(String storageCloudId) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageCloudId", storageCloudId);
    String jsonText = "";
    try {
      jsonText =
          doServerMethod(ApiConstants.URL_TB_STORAGE_STRATEGY_BY_STORAGE_CLOUD_ID, ApiConstants.GET, map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (jsonText == null || "".equals(jsonText)) return "false";
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && "false".equals(obj.get("message").toString())) {
      return Constant.NO_EXIST;
    } else if ("200".equals(obj.get("code").toString())
        && "true".equals(obj.get("message").toString())) {
      return Constant.EXIST;
    }
    return "false";
  }


  /**
   * 获取zookeeper主机名列表
   * @return
   */
  public static String getZookeeperHosts(){
    StorageCloud storageCloud = getStorageCloudByName(Config.STORAGE_CLOUD_NAME);
    return getZookeeperHosts(storageCloud.getId());
  }

  private static String getZookeeperHosts(String storageCloudId){
    String zkHosts = null;
    try {
      StringBuffer zkHostsBuffer = new StringBuffer();
      List<StorageServer> servers = getStorageCloudServerList(storageCloudId);
      for (int i = 0; i < servers.size(); i++) {
        StorageServer storageServer = servers.get(i);
        if(storageServer.getInternalHost()!=null){
          zkHostsBuffer.append(storageServer.getInternalHost() + ":" + storageServer.getInternalPort() + ",");
        }else {
          zkHostsBuffer.append(storageServer.getHost() + ":" + storageServer.getPort() + ",");
        }
      }
      zkHosts = zkHostsBuffer.toString();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return zkHosts;
  }

}
